package com.sylar.happyreader.net;

import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.text.TextUtils;

import com.sylar.happyreader.tools.Utils;

public class HttpHelper {
	private static final String tag=HttpHelper.class.getSimpleName();

	/**
	 * 放弃一个request，不再响应返回的数据
	 * @see HttpRequestBase#abort()
	 * @param request
	 * @return 返回是否放弃成功
	 */
	public static final boolean abortRequest(HttpRequestBase request)
	{
		if(request!=null)
		{
			request.abort();
			return true;
		}
		return false;
	}
	
	/**
	 * 解析HttpResponse响应为字符串
	 * @param resp
	 * @return 返回解析出来的字符串，UTF-8格式，可能为空
	 */
	public static String parserHttpResponseToString(HttpResponse resp)
	{
		byte[] data=parserHttpResponseToByte(resp);
		return Utils.byteToString(data);
	}
		
	/**
	 * 解析HttpResponse响应为byte数组
	 * @param resp
	 * @return 返回解析出来的byte数组，可能为null
	 */
	public static byte[] parserHttpResponseToByte(HttpResponse resp)
	{
		if(resp==null)return null;
		HttpEntity entity=resp.getEntity();
		if(entity==null)return null;
		try {
			return EntityUtils.toByteArray(entity);
		} catch (Exception e) {
			return null;
		}
	}
	
	
	/**
	 * 通过一个http的url生成一个HttpGet
	 * @param httpUrl url地址
	 * @return 返回生成的HttpGet，可能为null
	 */
	public static HttpGet get(String httpUrl)
	{
		if(!Utils.isStringBeHttpUrl(httpUrl))return null;		
		HttpGet get = new HttpGet(httpUrl);		
		return get;
	}
	
	/**
	 * 通过一个http的url生成一个HttpPost
	 * @param httpUrl url地址
	 * @param postData 需要post的数据
	 * @return 返回生成的HttpPost，可能为null
	 */
	public static HttpPost post(String httpUrl,String postData)
	{
		if(!Utils.isStringBeHttpUrl(httpUrl))return null;
		if(TextUtils.isEmpty(postData))return null;		
		HttpPost post = new HttpPost(httpUrl);		
		try
		{
			StringEntity se = new StringEntity(postData,HTTP.UTF_8);
			post.setEntity(se);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}		
		return post;
	}
	
	public static HttpPost postXml(String httpUrl,String postData)
	{
		if(!Utils.isStringBeHttpUrl(httpUrl))return null;
		if(TextUtils.isEmpty(postData))return null;		
		HttpPost post = new HttpPost(httpUrl);		
		try
		{
			StringEntity se = new StringEntity(postData,HTTP.UTF_8);
			se.setContentType("text/xml charset=utf-8");
			post.setEntity(se);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}		
		return post;
	}
	
	/**
	 * 处理HttpRequest，返回HttpResponse，已经考虑到apn接入点、wifi等所有的情况了
	 * @param context
	 * @param request 需要处理的request
	 * @return 返回HttpResponse响应，可能为null
	 */
	public static HttpResponse processHttpRequest(Context context,HttpRequestBase request)
	{
		if(request==null)return null;
		
		HttpClient httpClient = null;		
		try
		{			
			httpClient = new DefaultHttpClient();
			if(NetInfo.isWap(context))
			{
				HttpHost proxy = new HttpHost(NetInfo.getWapProxy(context), 80);
				httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
			}			
			HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),50 * 1000);
			HttpConnectionParams.setSoTimeout(httpClient.getParams(), 50 * 1000);
			request.addHeader("Content-Type", "application/x-www-form-urlencoded");
			request.addHeader("Charset", HTTP.UTF_8);
			request.addHeader("Connection", "Keep-Alive");
			return httpClient.execute(request);			
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return null;
	}
}
